//
//  ZXSaleActivityController.m
//  qianke
//
//  Created by ZX on 2017/6/29.
//  Copyright © 2017年 ZX. All rights reserved.
//

#import "ZXSaleActivityController.h"
#import "AddSaleActivityViewController.h"
#import "ZXsaleActivityCell.h"
#import "ZXsaleActivityModel.h"
#import "ZXsaleactivity2Cell.h"

@interface ZXSaleActivityController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIButton *addSaleActivityBtn;
@property (weak, nonatomic) IBOutlet UIView *emptyView;
- (IBAction)addSaleActivityBtnP_click:(id)sender;
/*tableview*/
@property(strong ,nonatomic) IBOutlet UITableView*saleActivityTableView;
/*teststatus*/
@property(copy ,nonatomic)NSMutableArray*status;
@property(assign ,nonatomic)BOOL isRefersh;
@property(assign ,nonatomic)int pageNo;
@property(copy ,nonatomic)NSString * Count;
@property(assign ,nonatomic)int pageSize;
@property(assign ,nonatomic)int upLoadNumber;
@property(assign ,nonatomic)BOOL NoMoreData;
@end

@implementation ZXSaleActivityController
@synthesize saleActivityTableView;
- (void)viewDidLoad {
    [super viewDidLoad];
    _isRefersh = NO;
    _pageSize = 10;
    _pageNo = 2;

    saleActivityTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"刷新");
        my_AppDelegate.isShow = YES;
        [self loadData];
    }];
    saleActivityTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    [self loadData];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [self loadData];
}
-(void)viewWillDisappear:(BOOL)animated{
    my_AppDelegate.isShow = NO;
}
-(void)loadData{

    if (!_status) {
        _status =[[NSMutableArray alloc]init];
    }
    NSString * strURL = [NSString stringWithFormat:@"%@scrmForApp/followDetail/%@/1/10",ZXBaseURL,_chanceId];
        strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:strURL];
        NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
        NSMutableURLRequest *mutableRequest = [request mutableCopy];
        [mutableRequest addValue:my_AppDelegate.access_token forHTTPHeaderField:accessTokenKey];
    NSLog(@"chanceId==%@",_chanceId);
    [mutableRequest addValue:[NSString stringWithFormat:@"%@",_chanceId] forHTTPHeaderField:@"chanceId"];
        request = [mutableRequest copy];
    [HTTPHelper GetHeadmutableRequest:request success:^(NSDictionary *data) {
            NSLog(@"%@",[data objectForKey:@"message"]);
            NSArray * arr = [data objectForKey:@"data"];
            dispatch_async(dispatch_get_main_queue(), ^{
                if (![arr isEqual:[NSNull null]]&&arr){
                    if (arr.count <10) {
                        saleActivityTableView.mj_footer.hidden = YES;
                    }
                }
                
            });
            NSLog(@"销售活动 == %@",arr);
            [_status removeAllObjects];
            if (![arr isEqual:[NSNull null]]&&arr)  {
                for (NSDictionary * dic in arr) {
                    ZXsaleActivityModel * model = [ZXsaleActivityModel new];
                    [model setValuesForKeysWithDictionary:dic];
                    [_status addObject:model];
                }
                NSLog(@"%@",_status);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [saleActivityTableView.mj_header endRefreshing];
                    if (_status.count) {
                        _emptyView.hidden = YES;
                    }
                    [saleActivityTableView reloadData];
                });
            }
        } failure:^(NSError *error) {
        [saleActivityTableView.mj_header endRefreshing];
        }];
    
    
    
}
-(void)loadMoreData{
    my_AppDelegate.isShow = YES;
    if (_Count.intValue%_pageSize) {
        _upLoadNumber = _Count.intValue/_pageSize + 1;
    }else _upLoadNumber = _Count.intValue/_pageSize;
    
    NSString *strURL = [NSString stringWithFormat:@"%@scrmForApp/followDetail/%@/%d/%d",ZXBaseURL,_chanceId,_pageNo,_pageSize];
    NSLog(@"strURL==%@",strURL);
    strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strURL];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
    NSMutableURLRequest *mutableRequest = [request mutableCopy];
    [mutableRequest addValue:my_AppDelegate.access_token forHTTPHeaderField:accessTokenKey];
    request = [mutableRequest copy];
    if (_pageNo > _upLoadNumber) {
        //        [myTableView.mj_footer setState:MJRefreshStateNoMoreData];
        //        return;
    }
    if (_NoMoreData) {
        [saleActivityTableView.mj_footer setState:MJRefreshStateNoMoreData];
        return;
    }
    NSLog(@"_pageNo==%d_upLoadNumber==%d",_pageNo,_upLoadNumber);
    [HTTPHelper GetHeadmutableRequest:request success:^(NSDictionary *data) {
        NSLog(@"%@",data);
        NSLog(@"%@",[data objectForKey:@"message"]);
        NSArray * arr = [data objectForKey:@"data"];
        NSString * msgCod = [data objectForKey:@"msgCod"];
        NSString * string = [data objectForKey:@"message"];
        if ([string containsString:@"无数据" ]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [saleActivityTableView.mj_footer endRefreshing];
                [saleActivityTableView.mj_footer setState:MJRefreshStateNoMoreData];
            });
            return ;
        }
        for (NSDictionary * dic in arr) {
            ZXsaleActivityModel * model = [ZXsaleActivityModel new];
            [model setValuesForKeysWithDictionary:dic];
             [_status addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            if (![msgCod isEqualToString:@"S0001"]) {
                _pageNo++;
            }else{
                _NoMoreData = YES;
                [saleActivityTableView.mj_footer endRefreshing];
                [saleActivityTableView.mj_footer setState:MJRefreshStateNoMoreData];
            }
        });
        
        NSLog(@"listArr.count==%lu",(unsigned long)_status.count);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [saleActivityTableView reloadData];
        });
        [saleActivityTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        [saleActivityTableView.mj_footer endRefreshing];
        
    }];
}
#pragma mark - tableView

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (_status.count<10) {
        saleActivityTableView.mj_footer.hidden = YES;
    }
    if (_status.count) {
        return _status.count+1;
    }else{
        return _status.count;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _status.count) {
        return 33;
    }
    ZXsaleActivityModel * model = self.status[indexPath.row];
    int height1 = [model.hContent stringSizeWithMaxSize:CGSizeMake(ScreenWidth*2/3, ScreenHeight) withFont:textfont].height;
    int height2 = [model.hAction stringSizeWithMaxSize:CGSizeMake(ScreenWidth*2/3, ScreenHeight) withFont:textfont].height;
    int height3 = [model.idea stringSizeWithMaxSize:CGSizeMake(ScreenWidth*2/3, ScreenHeight) withFont:textfont].height;
    if (height1<30) {
        height1 = 30;
    }
    if (height2<30) {
        height2 = 30;
    }
    if (height3<30) {
        height3 = 30;
    }
    return height1+height2+height3+220;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == _status.count) {
        ZXsaleactivity2Cell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if (!cell) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ZXsaleactivity2Cell" owner:nil options:nil] firstObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.backgroundColor = baseBackgroundColor;
        return cell;
    }
    ZXsaleActivityCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"ZXsaleActivityCell" owner:nil options:nil] firstObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    ZXsaleActivityModel * model = _status[indexPath.row];
    cell.status = model;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (IBAction)addSaleActivityBtnP_click:(id)sender {
    AddSaleActivityViewController * addVc = [AddSaleActivityViewController new];
    addVc.title = @"添加销售活动";
    addVc.chanceId = _chanceId;
    [SVProgressHUD dismiss];
    [self.navigationController pushViewController:addVc animated:YES];
}
- (IBAction)checkContentBtnClick:(id)sender {
    NSLog(@"查看详情");
}
- (IBAction)chengeOrderBtnClick:(id)sender {
    NSLog(@"转订单");
}
@end
